<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
      text-decoration: none;
      vertical-align: middle;
    }
    .box{
      width: 500px;
      height: 200px;
      border: 1px solid #ccc;
      margin: 20px auto;
      overflow: hidden;
      position: relative;
    }
    .box ul{
      width: 3000px;
      position: absolute;
      left: 0;
      top: 0;
    }
    .box li{
      float: left;
    }
    .btns{
      width: 100%;
      height: 60px;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      display: none;
    }
    .btns a{
      display: block;
      width: 40px;
      height: 60px;
      background-color: rgba(0,0,0,0.5);
      text-align: center;
      line-height: 60px;
      float: left;
      color: white;
    }
    .btns a:nth-child(2){
      float: right;
    }
    .page{
      position: absolute;
      right: 0;
      bottom: 5px;
    }
    .page span{
      display: inline-block;
      width: 24px;
      height: 24px;
      background-color: white;
      text-align: center;
      line-height: 24px;
      color: coral;
      margin-left: 4px;
      cursor: pointer;
    }
    .active{
      background-color: green !important;
      color: red !important;
    }
  
  </style>
</head>
<body>
  <div class="box">
    <ul>
      <li><a href="javascript:;"><img src="img/wf1.jpg" alt=""></a></li>
      <li><a href="javascript:;"><img src="img/wf2.jpg" alt=""></a></li>
      <li><a href="javascript:;"><img src="img/wf3.jpg" alt=""></a></li>
      <li><a href="javascript:;"><img src="img/wf4.jpg" alt=""></a></li>
      <li><a href="javascript:;"><img src="img/wf5.jpg" alt=""></a></li>
    </ul>
    <div class="btns">
      <a href="javascript:;">&lt;</a>
      <a href="javascript:;">&gt;</a>
    </div>
    <div class="page">
      <!-- 动态生成 -->
      <!-- <span class="active">1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
      <span>5</span> -->
    </div>
  </div>
  <script>
    //动态生成span标签
    var imgs = document.querySelectorAll('img');
    var page = document.querySelector('.page');
    console.log(page)
    for(var i = 0; i < imgs.length; i++){
      span = document.createElement('span');
      page.appendChild(span);
      span.innerText = i+1;
      i == 0 ? span.className = 'active' : span.className = '';
      //给每个页码注册点击事件
      span.onclick = span_click;
      span.myindex = i;
    }
    function span_click(){
      index = this.myindex;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*index,50);  
      for(var i = 0; i < imgs.length; i++){
        page.children[i].classList.remove('active');
      }
      page.children[index].classList.add('active');
    }
    //设置右边按钮
    var rbtn = document.querySelector('.btns a:last-child');
    var ul = document.querySelector('.box ul');
    console.log(ul);
    var index = 0;
    rbtn.onclick = function(){
      if(index == imgs.length){
        index = 0;
        ul.style.left = 0 + 'px';
      }
      index ++;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*index,50);
      // console.log(index)
      for(var i = 0; i < imgs.length; i++){
        page.children[i].classList.remove('active');
        if(index == imgs.length){
          page.children[0].classList.add('active');
        }else{
          page.children[index].classList.add('active');
        }
      }
    }
    var cfirst_li = ul.children[0].cloneNode(true);
    ul.appendChild(cfirst_li);
    //设置左边
    var lbtn = document.querySelector('.btns a:first-child');
    console.log(lbtn)
    lbtn.onclick = function(){
      if(index == 0){
        index = imgs.length;
        ul.style.left = -imgs[0].offsetWidth*index + 'px';
      }
      index --;
      console.log(index);
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*index,50);
      for(var i = 0; i < imgs.length; i++){
        page.children[i].classList.remove('active');
      }
      page.children[index].classList.add('active');
    }
    //设置自动播放 停止自动播放
    var box = document.querySelector('.box');
    var btns = document.querySelector('.btns');
    console.log(btns)
    var tid = null;
    box.onmouseenter = function(){
      btns.style.display = 'block';
      clearInterval(tid);
    };
    box.onmouseleave = function(){
      btns.style.display = 'none';
      tid = setInterval(function(){
        rbtn.click();
      },1000);
    };
    //动画
    function animate(element,current,target,step){
      if(element.timeID){
        clearInterval(element.timeID);
        element.timeID = null;
      }
      if(current > target){
        step = -Math.abs(step);
      }
      element.timeID = setInterval(function(){
        if(Math.abs(current - target) <= Math.abs(step)){
          current = target;
          clearInterval(element.timeID);
        }else{
          current += step;
        }
        element.style.left = current + 'px';
      },30)
      
    }
  </script>
</body>
</html>